package git

import (
	"github.com/chmodke/gitmm/config"
	"github.com/chmodke/gitmm/util"
)

// Clone is entry function
func Clone(remote config.Remote, repo config.Repo, workDir string, workBranch string, progress *util.Progress) bool {
	workPath, err := util.GetWorkDir(workDir)
	if err != nil {
		return false
	}
	builder := &util.CmdBuilder{}
	builder.Add("git").Add("clone")
	builder.Add("-o").Add(remote.Name).Add("--")
	builder.Add(repo.RemotePath(remote)).Add(repo.LocalPath(workPath))
	ret := util.Status(util.Execute("", builder.Build()))
	if !ret {
		return ret
	}
	progress.Next()

	builder.Reset()
	builder.Add("git").Add("checkout").Add(util.Nvl2(workBranch, repo.Branch, "master"))
	ret = util.Status(util.Execute(repo.LocalPath(workPath), builder.Build()))
	progress.Next()
	return ret
}
